---
- name: ensure packages required for imagefactory are installed (dnf)
  dnf: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - imagefactory
    - imagefactory-plugins
    - imagefactory-plugins-TinMan
    - git

- name: copy imagefactory.conf config file
  copy: src=imagefactory.conf dest=/etc/imagefactory/imagefactory.conf owner=root group=root mode=0644

- name: copy oz.cfg config file
  copy: src=oz.cfg dest=/etc/oz/oz.cfg owner=root group=root mode=0644

- name: copy imagefactory-kill-zombie script
  copy:
    src: imagefactory-kill-zombie
    dest: /usr/local/bin
    owner: root
    group: root
    mode: '0744'

- name: copy imagefactory-kill-zombie service
  copy:
    src: imagefactory-kill-zombie.service
    dest: /etc/systemd/system
  register: imagefactory_kill_zombie_service

- name: copy imagefactory-kill-zombie timer
  copy:
    src: imagefactory-kill-zombie.timer
    dest: /etc/systemd/system
  register: imagefactory_kill_zombie_timer

- name: hotfix imagefactory's REST api to allow file download
  copy: src=hotfix_imgfac_RESTv2.py dest=/usr/lib/python2.7/site-packages/imgfac/rest/RESTv2.py owner=root group=root mode=0644

# Workaround for https://github.com/redhat-imaging/imagefactory/issues/417
- name: fix issues in imagefactoryd.service
  copy:
    src: imagefactoryd.service
    dest: /etc/systemd/system/imagefactoryd.service
  register: imagefactory_service

- name: reload systemd
  command: systemctl daemon-reload
  when: imagefactory_service.changed or imagefactory_kill_zombie_service.changed or imagefactory_kill_zombie_timer.changed

- name: enable imagefactory
  service: name=imagefactoryd state=started enabled=yes

- name: enable imagefactory-kill-zombie.timer
  service:
    name: imagefactory-kill-zombie.timer
    state: started
    enabled: yes

- name: create directory for git clone
  file: path=/var/lib/fedoraqa state=directory owner=root group=root mode=1755

- name: Check out our imagefactory tools
  git:
    repo: https://pagure.io/taskotron/base_images.git
    dest: '/var/lib/fedoraqa/base_images'
    update: yes
    version: 'master'
  register: git_result
  changed_when: "git_result.after|default('after') != git_result.before|default('before')"

- name: create directory for configuration
  file: path=/etc/taskotron_base_images state=directory owner=root group=root mode=0755

- name: Generate configuration
  template: src={{ item }} dest=/etc/taskotron_base_images/config_server.ini owner=root group=root mode=0644
  with_first_found:
    - config_server.ini.j2.{{ deployment_type }}
    - config_server.ini.j2

- name: Create cronjob to build new Fedora {{ item }} images
  cron:
    name: "Build new Fedora {{ item }} taskotron_cloud images in ImageFactory"
    special_time: daily
    job: '/var/lib/fedoraqa/base_images/trigger_build/trigger_build.py build --config /etc/taskotron_base_images/config_server.ini --release {{ item }} --arch x86_64 --flavor="taskotron_cloud" --disksize="30G" > /dev/null'
  with_items:
    - 26
    - 27
    - 28
    - rawhide

- name: Create cronjob to report failed builds
  cron:
    name: "Report failed build in ImageFactory"
    special_time: daily
    job: '/var/lib/fedoraqa/base_images/report_failures/report_failures.py email --config /etc/taskotron_base_images/config_server.ini > /dev/null'

- name: Create cronjob to prune old builds in ImageFactory
  cron:
    name: "Prune old builds in ImageFactory"
    special_time: daily
    job: '/var/lib/fedoraqa/base_images/prune_images/prune_images.py imgfac-old --config /etc/taskotron_base_images/config_server.ini --keep 3  > /dev/null'
